package com.reservation.service.impl;

import com.reservation.dto.user.home.UpdateInfoDTO;
import com.reservation.mapper.HomeMapper;
import com.reservation.service.HomeService;
import com.reservation.utils.CurrentHolderUtil;
import com.reservation.vo.user.home.GetSystemInfoVO;
import com.reservation.vo.user.home.GetTypeCountVO;
import com.reservation.vo.user.home.GetWeekCountArrVO;
import com.reservation.vo.user.home.GetWeekCountVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j

public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;

    public boolean updateInfo(UpdateInfoDTO updateInfoDTO ) {

        updateInfoDTO.setId(CurrentHolderUtil.getCurrentId());

        return homeMapper.updateInfo(updateInfoDTO) > 0;
    }

    @Override
    public GetSystemInfoVO getSystemInfo() {

        int userId = CurrentHolderUtil.getCurrentId();
        GetSystemInfoVO getSystemInfoVO = new GetSystemInfoVO();

        getSystemInfoVO.setCount(homeMapper.getOrderCount(userId));
        getSystemInfoVO.setTotal(homeMapper.getTotalMoney(userId)- homeMapper.getRefundMoney(userId));
        String dateStr = homeMapper.getFirstDate(userId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
        LocalDate targetDate = dateTime.toLocalDate();
        getSystemInfoVO.setDate(String.valueOf(targetDate));
        LocalDate currentDate = LocalDate.now();
        int daysDifference = (int) ChronoUnit.DAYS.between(targetDate, currentDate);
        getSystemInfoVO.setDays(daysDifference);

        return getSystemInfoVO;
    }

    @Override
    public List<GetTypeCountVO> getTypeCount() {

        int userId = CurrentHolderUtil.getCurrentId();
        List<GetTypeCountVO> getTypeCountVOList = homeMapper.getTypeCount(userId);

        return getTypeCountVOList;
    }

    @Override
    public GetWeekCountArrVO getWeekCountArr() {

        int userId = CurrentHolderUtil.getCurrentId();
        List<GetWeekCountVO> list =homeMapper.getWeekCount(userId);

        Map<String, Integer> dateCountMap = list.stream()
                .collect(Collectors.toMap(
                        GetWeekCountVO::getDate,
                        GetWeekCountVO::getCount
                ));

        LocalDate endDate = LocalDate.now().minusDays(1); // 昨天
        LocalDate startDate = endDate.minusDays(6); // 7天前（包含昨天共7天）

        List<String> dateList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd");
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            dateList.add(date.format(outputFormatter));
            countList.add(dateCountMap.getOrDefault(date.format(inputFormatter), 0));
        }
        String[] dateArray = dateList.toArray(new String[0]);
        Integer[] countArray = countList.toArray(new Integer[0]);

        GetWeekCountArrVO arr = new GetWeekCountArrVO(dateArray,countArray);

        return arr;
    }
}
